﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using IceDog.EFCore.Models.Blog;
using IceDog.EFCore.WebApiServer.Context;

namespace IceDog.EFCore.WebApiServer.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class BlogArticlesController : ControllerBase
    {
        private readonly DioBlogContext _context;

        public BlogArticlesController(DioBlogContext context)
        {
            _context = context;
        }

        // GET: api/BlogArticles
        [HttpGet]
        public IEnumerable<BlogArticle> GetBlogArticles()
        {
            return _context.BlogArticles;
        }

        // GET: api/BlogArticles/5
        [HttpGet("{id}")]
        public async Task<IActionResult> GetBlogArticle([FromRoute] int id)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var blogArticle = await _context.BlogArticles.FindAsync(id);

            if (blogArticle == null)
            {
                return NotFound();
            }

            return Ok(blogArticle);
        }

        // PUT: api/BlogArticles/5
        [HttpPut("{id}")]
        public async Task<IActionResult> PutBlogArticle([FromRoute] int id, [FromBody] BlogArticle blogArticle)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != blogArticle.ArticleId)
            {
                return BadRequest();
            }

            _context.Entry(blogArticle).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!BlogArticleExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return NoContent();
        }

        // POST: api/BlogArticles
        [HttpPost]
        public async Task<IActionResult> PostBlogArticle([FromBody] BlogArticle blogArticle)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            _context.BlogArticles.Add(blogArticle);
            await _context.SaveChangesAsync();

            return CreatedAtAction("GetBlogArticle", new { id = blogArticle.ArticleId }, blogArticle);
        }

        // DELETE: api/BlogArticles/5
        [HttpDelete("{id}")]
        public async Task<IActionResult> DeleteBlogArticle([FromRoute] int id)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var blogArticle = await _context.BlogArticles.FindAsync(id);
            if (blogArticle == null)
            {
                return NotFound();
            }

            _context.BlogArticles.Remove(blogArticle);
            await _context.SaveChangesAsync();

            return Ok(blogArticle);
        }

        private bool BlogArticleExists(int id)
        {
            return _context.BlogArticles.Any(e => e.ArticleId == id);
        }
    }
}